import EncryptStatus from "@/types/encryptStatus";
import RsaUtil from "@/utils/secret/rsaUtil";
import {TimeUtil} from "@/utils/timeUtil";
import TypeUtil from "@/utils/typeUtil";
import type { InternalAxiosRequestConfig } from "axios";
let defaultEncryptParams=false;
let defaultEncryptBody=false;
//加密请求
function encryptRequest(req:InternalAxiosRequestConfig<any>){
    req.headers.set("requestDate",TimeUtil.getMilliseconds())//设置请求的发送时间
    if(req.headers[EncryptStatus.encryptParams]===undefined) req.headers[EncryptStatus.encryptParams]=defaultEncryptParams;
    if(req.headers[EncryptStatus.encryptBody]===undefined) req.headers[EncryptStatus.encryptBody]=defaultEncryptBody;
    if(req.headers[EncryptStatus.encryptParams]==true){
        if(req.params==null||TypeUtil.isEmpytObject(req.params)){
          req.headers[EncryptStatus.encryptParams]=false;
        }
        else req.params=RsaUtil.encryptOjbect(req.params);
    }
    if(req.headers[EncryptStatus.encryptBody]==true){
      if(req.data==null||TypeUtil.isEmpytObject(req.data)){
        req.headers[EncryptStatus.encryptBody]=false;
      }
      else req.data=RsaUtil.encrypt(req.data);
    } 
    return req;
}
function setAuthorization(req:InternalAxiosRequestConfig<any>,auth:string){
    req.headers.set("Authorization",auth);
    return req;
}
